package action.AZ20;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import services.AZ20.FilehploadServices;
import com.util.Component;
import com.Const;
import com.PublicSystem;
import com.syslog.SysLogServices;
/**
 * 文件上传 Action
 * @author Administrator
 *
 */
@Controller
@RequestMapping("file.do")
public class FilehploadAction extends HttpServlet{	
	SysLogServices log = SysLogServices.getInstance();
	PublicSystem sys = PublicSystem.getInstance();
	FilehploadServices ls = new FilehploadServices();
	private static final long serialVersionUID = 1L;
	/**
	 * 文件上传
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(params = "method=dofile")
	public void dofile(HttpServletRequest request, HttpServletResponse response)
			throws Exception {		
		//创建一个通用的多部分解析器  
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        //判断 request 是否有文件上传,即多部分请求  
        if(multipartResolver.isMultipart(request)) {
        	//转换成多部分request
        	MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
        	//取得request中的所有文件名  
        	Iterator<String> iter = multiRequest.getFileNames();       
        	int i = 1;
        	String eventid = "";
        	String userid=Component.getSession("userid", request);
        	while(iter.hasNext()) {
        		//记录上传过程起始时的时间，用来计算上传时间  
        		int pre = (int) System.currentTimeMillis();
        		//取得上传文件  
        		MultipartFile file = multiRequest.getFile(iter.next());
        		if(file != null){
        			//取得当前上传文件的文件名称 
        			String FileName = file.getOriginalFilename();
        			//如果名称不为"",说明该文件存在，否则说明该文件不存在  
        			if(FileName.trim() != ""){
        				//定义上传路径 
        				Date de =new Date();
	    				SimpleDateFormat sp =new  SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
	    				String s_time=sp.format(de);	    				
	    				int radNum = (int)(Math.random() * 100000);
	    				String encodeFileName = s_time + "_" + radNum + "." + "xlsx";
	    			
        				String path = Const.server_path + "/importer/" + encodeFileName;
        				File localFile = new File(path);
        				if(!localFile.exists()) {
	    					localFile.mkdirs();
	    				}	
        				file.transferTo(localFile);
        				List list = procomponent.Excelformat.readExcel(localFile);
        				eventid = ls.insertsql(list, "AZ20");
        			}
        			HashMap obj = new HashMap();
        			obj.put("domainid", "SBK");  //分系统号，见sys_log_bm
        			obj.put("log_type", "T07");   //日志类别，见sys_log_bm
        			obj.put("userid", userid); //操作人
        			obj.put("rwh", Component.getSession("rwid", request));	      //任务号（菜单号），取Component.getSession("rwid", request),若特殊则为空为自定义
        			obj.put("content", FileName + "的导入");  //日志内容，自己组合拼装
        			obj.put("czqd", "Q1");	      //操作渠道，见sys_log_bm
        			obj.put("czrlb", "L01");      //操作人类别，见sys_log_bm
        			obj.put("datain", "文件名" + FileName); //输入参数
        			obj.put("dataout", "success");//输出参数
        			log.sysLogMap(obj, request);
        		}
        		//记录上传该文件后的时间  
        		int finaltime = (int) System.currentTimeMillis();
           }
           Component.print(eventid, response);
         }
    }
}